<template>
  <!-- 人员添加记录 -->
  <div class="app-container horizontal">
    <div class="form-container">
      <el-form ref="queryForm" :inline="true" label-width="80px" :model="filters">
        <el-form-item label="用户账号" prop="loginName">
          <el-input
            v-model="filters.loginName"
            size="small"
            placeholder
            clearable
          />
        </el-form-item>
        <el-form-item label="用户类型" prop="sysUserType">
          <el-select v-model="filters.sysUserType" clearable size="small">
            <el-option
              v-for="dict in PersonnelType"
              :key="dict.typeNum"
              :label="dict.typeName"
              :value="dict.typeNum"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="手机号码" prop="phone">
          <el-input
            v-model="filters.phone"
            size="small"
            placeholder
            clearable
          />
        </el-form-item>
        <!-- 高级设置一栏 start -->
        <el-form-item>
          <slot name="label">
            <i class="highLevelStyle" :class="highLevelSet===true?'el-icon-arrow-up':'el-icon-arrow-down'" @click="highLevelSet = !highLevelSet" />
          </slot>
        </el-form-item>
        <el-row v-if="highLevelSet">
          <el-form-item label="用户状态" prop="status">
            <el-select v-model="filters.status" clearable size="small">
              <el-option
                v-for="dict in statusOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="创建时间">
            <el-date-picker
              v-model="dateRange"
              size="small"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :clearable="false"
            />
          </el-form-item>
        </el-row>
      </el-form>
    </div>
    <!-- 高级设置一栏 end -->
    <div class="btns-group">
      <el-button v-clickForbidden icon="el-icon-search" size="middle" @click="handleQuery()">搜索</el-button>
      <el-button v-clickForbidden icon="el-icon-refresh" size="middle" @click="resetQuery()">重置</el-button>
      <el-button size="middle" :disabled="single" @click="handleDelete()">
        <svg-icon icon-class="revoke" :disabled="single" /> 撤销邀请
      </el-button>
      <span class="personal-type">
        <el-button icon="el-icon-arrow-left" size="middle" @click="handleReturn()">返回</el-button>
      </span>
    </div>
    <!-- 表格展示start -->
    <el-table
      ref="multipleTable"
      v-loading="loading"
      :data="sysUserData"
      :row-style="rowClass"
      style="width: 100%;"
      stripe
      class="table-list"
      :class="highLevelSet===true ? (needTagsView===true ? 'el-body-page-roll-tag-height' : 'el-body-page-roll-height') :
        (needTagsView===true ? 'el-body-page-roll-tag' : 'el-body-page-roll')"
      @selection-change="handleSelectionChange"
      @row-click="rowChecked"
    >
      <el-table-column type="selection" width="38" align="center" />
      <el-table-column type="index" label="序号" width="50" align="center" />
      <el-table-column
        prop="sysAccount.name"
        label="用户账号"
        min-width="150"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        prop="sysAccount.surveyType"
        label="类型"
        min-width="150"
        align="center"
        :show-overflow-tooltip="true"
        :formatter="(row) => formatterPersonSupperType(row)"
      />
      <el-table-column
        prop="sysAccount.phone"
        label="手机号码"
        min-width="140"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column prop="status" width="150" label="状态" align="center" :formatter="userAddEnterpriseStatusFormat" />
    </el-table>
    <!-- 表格展示end-->
    <!-- 分页-->
    <pagination
      :total="total"
      :page.sync="filters.pageNum"
      :limit.sync="filters.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { personTypeListNoPermission } from '@/api/superviseBaseInfo/typeManage/personnelType'
import { sysApplyRecord, updateAccountStatus } from '@/api/system/enterpriseRecord'
import { getDicts } from '@/api/system-tool/dict/data'
import { getSysDicts } from '@/api/common/getSysDicts'
export default {
  data() {
    return {
      loading: false,
      // 高级设置显示隐藏
      highLevelSet: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 选中表格对话框的存储的值
      multipleSelection: [],
      // 高亮样式
      selectRow: [],
      // 查询参数
      filters: {
        pageNum: 1,
        pageSize: this.fristPageSize,
        userName: '',
        phone: '',
        status: '',
        sysUserType: '',
        accountType: '1',
        enterpriseId: this.$store.state.user.userInfo.enterpriseId
      },
      // 人员类型
      PersonnelType: [],
      // 性别信息
      sexOptions: [],
      // 状态信息
      statusOptions: [],
      // 日期范围
      dateRange: [],
      // 分页总条数
      total: 0,
      // 人员数据
      sysUserData: [],
      // 人员申请加入企业状态
      userAddEnterpriseStatus: []
    }
  },
  computed: {
    ...mapState({
      needTagsView: state => state.settings.tagsView
    }),
    // 高级搜索显示隐藏
    handleTagShow() {
      return this.highLevelSet
    }
  },
  watch: {
  },
  mounted() {
    this.listPersonSupperType()
    this.getList()
    // 用户性别
    this.getDicts('sys_user_sex').then(response => {
      this.sexOptions = response.data.rows
    })
    // 用户状态信息
    getDicts('sys_normal_disable').then(response => {
      this.statusOptions = [{ dictValue: '', dictLabel: '全部' }, ...response.data.rows]
    })
    // 人员申请加入企业状态
    getSysDicts('sys_person_add_enterprise_status').then(res => {
      this.userAddEnterpriseStatus = res.data.rows
    })
  },
  methods: {
    // 获取人员类型
    async  listPersonSupperType() {
      const { data: { rows }} = await personTypeListNoPermission()
      this.PersonnelType = [{ typeNum: '', typeName: '全部' }, ...rows]
    },
    // 用户性别字典翻译
    sexFormat(row, column) {
      return this.selectDictLabel(this.sexOptions, row.sex)
    },
    // 用户状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status)
    },
    // 用户状态字典翻译
    userAddEnterpriseStatusFormat(row, column) {
      return this.selectDictLabel(this.userAddEnterpriseStatus, row.status)
    },
    // 回显人员类型
    formatterPersonSupperType(row) {
      if (row && row.sysAccount && row.sysAccount.surveyType) {
        const items = this.PersonnelType.filter(item => {
          return item.typeNum === row.sysAccount.surveyType
        })
        return items.length > 0 ? items[0].typeName : '-'
      }
    },
    // 单击每行时每行的复选框选中
    rowChecked(row) {
      const selectData = this.multipleSelection
      this.$refs.multipleTable.clearSelection()
      if (selectData.length === 1) {
        selectData.forEach(item => {	// 判断 如果当前的一行被勾选, 再次点击的时候就会取消选中
          if (item === row) {
            this.$refs.multipleTable.toggleRowSelection(row, false)
          } else {
            this.$refs.multipleTable.toggleRowSelection(row, true)
          }
        })
      } else {
        this.$refs.multipleTable.toggleRowSelection(row, true)
      }
    },
    // 点击表格前面选框时触发的事件
    handleSelectionChange(val) {
      if (JSON.stringify(val[0]) === '{}') {
        this.$refs.multipleTable.clearSelection()
        val = []
      }
      // 0代表正常状态，1代表停用状态
      this.multipleSelection = val
      // eslint-disable-next-line no-prototype-builtins
      if (this.multipleSelection.length === 1 && this.multipleSelection[0].hasOwnProperty('userId') && val[0].status === '2') {
        this.single = false
      } else {
        this.single = true
      }
      this.selectRow = []
      if (this.multipleSelection.length > 0) {
        this.multipleSelection.forEach((item, index) => {
          this.selectRow.push(this.sysUserData.indexOf(item))
        })
      }
      if (val.length > 1) {
        this.$refs.multipleTable.clearSelection()
        this.$refs.multipleTable.toggleRowSelection(val.pop())
      }
    },
    // 高亮样式
    rowClass({ row, rowIndex }) {
      if (this.selectRow.includes(rowIndex)) {
        return { 'background-color': 'rgba(185,221,249,.75)' }
      }
    },
    // list表格填充空白行
    addTaleData() {
      if (this.sysUserData.length < this.fristPageSize) {
        var arr = {}
        this.sysUserData.push(arr)
        this.addTaleData()
      }
    },
    // 获取列表
    async getList() {
      this.loading = true
      try {
        const { data: { rows, total }} = await sysApplyRecord(this.changeDateRange(this.filters, this.dateRange))
        this.sysUserData = rows
        this.total = total
        // this.addTaleData()
        this.loading = false
      } catch {
        this.loading = false
      }
    },
    // 撤销申请
    handleDelete() {
      const { sysAccount, id } = this.multipleSelection[0]
      this.$confirm(`确认要撤销邀请 ${sysAccount.name}吗?`, '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const data = { status: '3', id }
          await updateAccountStatus(data)
          this.msgSuccess('撤销成功')
          this.getList()
        })
        .catch(() => {})
    },
    // 搜索方法
    handleQuery() {
      this.filters.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.dateRange = []
      this.handleQuery()
    },
    // 返回
    handleReturn() {
      this.$router.go(-1)
    }
  }
}
</script>

